﻿using System;
using System.Runtime.InteropServices;
using System.Text;

namespace MuNet
{
    /// <summary>
    /// INI帮助类
    /// </summary>
    public class MuIni
    {
        /// <summary>
        /// 当前的INI文件路径，只读
        /// </summary>
        private readonly string s_path;

        /// <summary>
        /// 禁止无参数构造
        /// </summary>
        private MuIni()
        {
            throw new Exception();
        }

        /// <summary>
        /// 构造函数，隐藏所有其他构造
        /// </summary>
        /// <param name="path">指定INI文件路径</param>
        public MuIni(string path)
        {
            this.s_path = path;
        }

        /// <summary>
        /// 外部实现的写入INI
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键名</param>
        /// <param name="val">键值</param>
        /// <param name="filePath">INI文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        /// <summary>
        /// 写入INI
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键名</param>
        /// <param name="value">键值</param>
        /// <returns></returns>
        public bool Set(string section, string key, string value)
        {
            try
            {
                WritePrivateProfileString(section, key, value, this.s_path);
                return true;
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            
        }

        /// <summary>
        /// 将指定键名的值制空
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键名</param>
        /// <returns></returns>
        public bool Empty(string section, string key)
        {
            return this.Set(section, key, "");
        }

        /// <summary>
        /// 删除指定键名
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键名</param>
        /// <returns></returns>
        public bool Delete(string section, string key)
        {
            return this.Set(section, key, null);
        }

        /// <summary>
        /// 删除指定节点
        /// </summary>
        /// <param name="section">要删除的节点名</param>
        /// <returns></returns>
        public bool Delete(string section)
        {
            return this.Set(section, null, null);
        }

        /// <summary>
        /// 清空当前INI文件(测试发现无效)
        /// </summary>
        /// <returns></returns>
        public bool Clear()
        {
            return this.Set(null, null, null);
        }

        /// <summary>
        /// 外部实现的读取INI
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="def"></param>
        /// <param name="retVal"></param>
        /// <param name="size"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// 获取指定键名值
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="key">键名</param>
        /// <returns></returns>
        public string Get(string section, string key)
        {
            StringBuilder temp = new StringBuilder(255);
            GetPrivateProfileString(section, key, "", temp, 255, this.s_path);
            return temp.ToString();
        }

        /// <summary>
        /// 外部实现的读取INI
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="defVal"></param>
        /// <param name="retVal"></param>
        /// <param name="size"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);

        /// <summary>
        /// 获取指定键名值数组
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="key">键名</param>
        /// <returns></returns>
        public byte[] GetValues(string section, string key)
        {
            byte[] temp = new byte[255];
            GetPrivateProfileString(section, key, "", temp, 255, this.s_path);
            return temp;
        }
    }
}
